#ifndef YCCOREOPENUSER_H
#define YCCOREOPENUSER_H
#include "useropeninfo.h"
#include <QObject>
#include <QUrl>

namespace yc {

class OpenUser : public QObject, public UserOpenInfo
{
    Q_OBJECT
    Q_PROPERTY(int id READ id CONSTANT FINAL)
    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged FINAL)
    Q_PROPERTY(QString nickname READ nickname WRITE setNickname NOTIFY nicknameChanged FINAL)
    Q_PROPERTY(int gender READ gender WRITE setGender NOTIFY genderChanged FINAL)
    Q_PROPERTY(QString avatar READ avatar WRITE setAvatar NOTIFY avatarChanged FINAL)
    Q_PROPERTY(QString address READ address WRITE setAddress NOTIFY addressChanged FINAL)
    Q_PROPERTY(QString profile READ profile WRITE setProfile NOTIFY profileChanged FINAL)
    Q_PROPERTY(QString cover READ cover WRITE setCover NOTIFY coverChanged FINAL)
    Q_PROPERTY(int status READ status WRITE setStatus NOTIFY statusChanged FINAL)
    Q_PROPERTY(QString displayName READ displayName NOTIFY displayNameChanged FINAL)
    Q_PROPERTY(QUrl coverUrl READ coverUrl WRITE setCoverUrl NOTIFY coverUrlChanged FINAL)
    Q_PROPERTY(QUrl avatarUrl READ avatarUrl WRITE setAvatarUrl NOTIFY avatarUrlChanged FINAL)

public:
    explicit OpenUser(QObject* parent = nullptr) : QObject(parent) { }
    OpenUser(int id, const QString& name, QObject* parent = nullptr)
        : QObject(parent), UserOpenInfo(id, name)
    {
    }
    explicit OpenUser(const QJsonObject& jo, QObject* parent = nullptr)
        : QObject(parent), UserOpenInfo(jo)
    {
    }

    void setName(const QString& name)
    {
        UserOpenInfo::setName(name);
        Q_EMIT nameChanged();
    }
    void setNickname(const QString& nickname)
    {
        UserOpenInfo::setNickname(nickname);
        Q_EMIT nicknameChanged();
    }
    void setGender(int gender)
    {
        UserOpenInfo::setGender(gender);
        Q_EMIT genderChanged();
    }
    void setAvatar(const QString& avatar)
    {
        UserOpenInfo::setAvatar(avatar);
        Q_EMIT avatarChanged();
    }
    void setAddress(const QString& address)
    {
        UserOpenInfo::setAddress(address);
        Q_EMIT addressChanged();
    }
    void setProfile(const QString& profile)
    {
        UserOpenInfo::setProfile(profile);
        Q_EMIT profileChanged();
    }
    void setCover(const QString& cover)
    {
        UserOpenInfo::setCover(cover);
        Q_EMIT coverChanged();
    }
    void setStatus(int status)
    {
        UserOpenInfo::setStatus(status);
        Q_EMIT statusChanged();
    }

    QString displayName() const { return nickname().isEmpty() ? name() : nickname(); }
    const QUrl& coverUrl() const { return cover_url_; }
    void setCoverUrl(const QUrl& url)
    {
        if (cover_url_ != url) {
            cover_url_ = url;
            Q_EMIT coverUrlChanged();
        }
    }
    const QUrl& avatarUrl() const { return avatar_url_; }
    void setAvatarUrl(const QUrl& url)
    {
        if (avatar_url_ != url) {
            avatar_url_ = url;
            Q_EMIT avatarUrlChanged();
        }
    }

    void updateData(const QJsonObject& jo)
    {
        UserOpenInfo::updateData(jo);
        Q_EMIT nameChanged();
        Q_EMIT nicknameChanged();
        Q_EMIT genderChanged();
        Q_EMIT avatarChanged();
        Q_EMIT addressChanged();
        Q_EMIT profileChanged();
        Q_EMIT coverChanged();
        Q_EMIT statusChanged();
        Q_EMIT displayNameChanged();
    }

Q_SIGNALS:
    void nameChanged();
    void nicknameChanged();
    void genderChanged();
    void avatarChanged();
    void addressChanged();
    void profileChanged();
    void coverChanged();
    void statusChanged();
    void displayNameChanged();
    void coverUrlChanged();
    void avatarUrlChanged();

private:
    QUrl cover_url_;
    QUrl avatar_url_;
};

} // namespace yc

Q_DECLARE_METATYPE(yc::OpenUser)

#endif // YCCOREOPENUSER_H
